// ============================================================================
//
// Copyright (C) 2006-2007 Dengues
//
// Google Group: http://groups.google.com/group/dengues
// QQ Group: 24885404
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
//
// ============================================================================
package org.dengues.rcp.splashHandlers;

import org.dengues.core.DenguesCorePlugin;
import org.dengues.core.IDesignerCoreService;
import org.dengues.core.prefs.IDenguesPrefsConstant;
import org.dengues.designer.core.codegen.config.JETCodeGeneratorEmittersPoolFactory;
import org.dengues.designer.core.i18n.Messages;
import org.dengues.ui.DenguesUiPlugin;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.splash.AbstractSplashHandler;

/**
 * @since 3.3
 * 
 * Qiang.Zhang.Adolf@gmail.com class global comment. Detailled comment <br/>
 * 
 * $Id: Dengues.epf Qiang.Zhang.Adolf@gmail.com 2008-4-9 qiang.zhang $
 * 
 */
public class InteractiveSplashHandler extends AbstractSplashHandler {

    private static final String DONE = "done";

    // private final static int F_LABEL_HORIZONTAL_INDENT = 145;

    private final static int F_COLUMN_COUNT = 3;

    private Composite fCompositeLogin;

    private Label zLabel;

    private boolean fAuthenticated;

    private String zlabelString = Messages.getString(
            "CodeGeneratorEmittersPoolFactory.JobLabel", "...(" + JETCodeGeneratorEmittersPoolFactory.PREC + "%)");//$NON-NLS-1$

    /**
     * Qiang.Zhang.Adolf@gmail.com InteractiveSplashHandler constructor comment.
     */
    public InteractiveSplashHandler() {
        fCompositeLogin = null;
        zLabel = null;
        fAuthenticated = false;
    }

    /*
     * (non-Javadoc)
     * 
     * @see org.eclipse.ui.splash.AbstractSplashHandler#init(org.eclipse.swt.widgets.Shell)
     */
    @Override
    public void init(final Shell splash) {
        // Store the shell
        super.init(splash);
        // Configure the shell layout
        configureUISplash();
        // Create UI
        createUI();
        // Force the splash screen to layout
        splash.layout(true);
        new Thread(new Runnable() {

            /*
             * (non-Javadoc)
             * 
             * @see java.lang.Runnable#run()
             */
            public void run() {
                IDesignerCoreService designerCoreService = DenguesCorePlugin.getDefault().getDesignerCoreService();
                JETCodeGeneratorEmittersPoolFactory.initializeNotJob();
                if (JETCodeGeneratorEmittersPoolFactory.PREC < 90) {
                    JETCodeGeneratorEmittersPoolFactory.PREC += 10;
                }
                designerCoreService.getJavaProcessor().synDenguesApi();
                if (DenguesUiPlugin.getDefault().getPreferenceStore().getBoolean(IDenguesPrefsConstant.REFRESH_BLOCKS)) {
                    designerCoreService.refreshBlocks();
                }
                JETCodeGeneratorEmittersPoolFactory.PREC = 99;
                fAuthenticated = true;
            }
        }).start();
        // Keep the splash screen visible and prevent the RCP application from
        // loading until the close button is clicked.
        doEventLoop();
    }

    /**
     * 
     */
    private void doEventLoop() {
        Shell splash = getSplash();
        while (fAuthenticated == false) {
            if (zLabel != null) {
                zlabelString = Messages.getString(
                        "CodeGeneratorEmittersPoolFactory.JobLabel", "...(" + JETCodeGeneratorEmittersPoolFactory.PREC + "%)");//$NON-NLS-1$
                zLabel.setText(zlabelString);
                System.out.println(JETCodeGeneratorEmittersPoolFactory.PREC);
                fCompositeLogin.layout();
            }
            if (splash.getDisplay().readAndDispatch() == false) {
                splash.getDisplay().sleep();
            }
        }
    }

    /**
     * 
     */
    private void createUI() {
        // Create the login panel
        createUICompositeLogin();
        // Create the blank spanner
        createUICompositeBlank();
        // Create the user name label
        createUILabelUserName();
    }

    /**
     * 
     */
    private void createUILabelUserName() {
        // Create the label
        zLabel = new Label(fCompositeLogin, SWT.CENTER);
        zLabel.setText(zlabelString);
        // Configure layout data
        GridData data = new GridData(GridData.FILL_HORIZONTAL);
        data.horizontalIndent = 0;
        zLabel.setForeground(getSplash().getDisplay().getSystemColor(SWT.COLOR_BLUE));
        zLabel.setLayoutData(data);
    }

    /**
     * 
     */
    private void createUICompositeBlank() {
        Composite spanner = new Composite(fCompositeLogin, SWT.NONE);
        GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
        data.horizontalSpan = F_COLUMN_COUNT;
        spanner.setLayoutData(data);
    }

    /**
     * 
     */
    private void createUICompositeLogin() {
        // Create the composite
        fCompositeLogin = new Composite(getSplash(), SWT.BORDER);
        GridLayout layout = new GridLayout();
        fCompositeLogin.setLayout(layout);
    }

    /**
     * 
     */
    private void configureUISplash() {
        // Configure layout
        FillLayout layout = new FillLayout();
        getSplash().setLayout(layout);
        // Force shell to inherit the splash background
        getSplash().setBackgroundMode(SWT.INHERIT_DEFAULT);

    }

}
